<script setup name="">
import { getToken } from '@/utils/auth'
import {
  listExcel,
  delExcelInfo,
  updateCredits,
  addCredits
} from '@/api/commission/rebateXS'
const { proxy } = getCurrentInstance()
const userList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const title = ref('')
/*** 用户导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: '',
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: 'Bearer ' + getToken() },
  // 上传的地址
  url:
    import.meta.env.VITE_APP_BASE_API +
    '/credits/statementOfAccount/importStatementOfAccount'
})

const data = reactive({
  form: {},
  queryParams: {
    meetingName: undefined // 台账名称
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询用户列表 */
function getList () {
  loading.value = true
  listExcel(proxy.addDateRange(queryParams.value)).then(res => {
    loading.value = false
    userList.value = res.data
  })
}

/** 搜索按钮操作 */
function handleQuery () {
  getList()
}
/** 重置按钮操作 */
function resetQuery () {
  proxy.resetForm('queryRef')
  queryParams.value.meetingName = undefined
  handleQuery()
}
/** 删除按钮操作 */
function handleDelete (row) {
  proxy.$modal
    .confirm('是否确认删除编号为"' + row.id + '"的数据项？')
    .then(function () {
      return delExcelInfo({ id: row.id })
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 重置操作表单 */
function reset () {
  form.value = {}
  proxy.resetForm('commission')
}
/** 取消按钮 */
function cancel () {
  open.value = false
  reset()
}
/** 新增按钮操作 */
function handleAdd () {
  reset()
  open.value = true
  title.value = '添加'
}
/** 修改按钮操作 */
function handleUpdate (row) {
  reset()
  form.value = JSON.parse(JSON.stringify(row)) // 赋值需要深拷贝
  open.value = true
  title.value = '修改'
}
/** 提交按钮 */
function submitForm () {
  proxy.$refs['commission'].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateCredits({
          ...form.value
        }).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addCredits(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

getList()
</script>

<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="台账名称" prop="meetingName">
        <el-input
          v-model="queryParams.meetingName"
          placeholder="请输入台账名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" border max-height="650">
      <el-table-column
        label="台账名称"
        align="center"
        prop="meetingName"
        :show-overflow-tooltip="true"
        width="200"
      />
      <el-table-column
        label="账期"
        align="center"
        prop="accountPeriod"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="预估在网率"
        align="center"
        prop="onlineRate"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="出账系数"
        align="center"
        prop="coefficient"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="预结"
        align="center"
        prop="preSettlement"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="店铺预结"
        align="center"
        prop="deptPreSettlement"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="话费预估"
        align="center"
        prop="estimatedPhoneBill"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="类型"
        align="center"
        prop="type"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        :model="form"
        :rules="rules"
        ref="commission"
        label-width="100px"
      >
        <el-form-item label="台账名称" prop="meetingName">
          <el-input v-model="form.meetingName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="账期" prop="accountPeriod">
          <el-input
            v-model.number="form.accountPeriod"
            placeholder="请输入"
            type="number"
          />
        </el-form-item>
        <el-form-item label="预估在网率" prop="onlineRate">
          <el-input v-model="form.onlineRate" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="出账系数" prop="coefficient">
          <el-input
            v-model="form.coefficient"
            placeholder="请输入"
            type="number"
          />
        </el-form-item>
        <el-form-item label="预结" prop="preSettlement">
          <el-input v-model="form.preSettlement" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="店铺预结" prop="deptPreSettlement">
          <el-input v-model="form.deptPreSettlement" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="话费预估" prop="estimatedPhoneBill">
          <el-input v-model="form.estimatedPhoneBill" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
